﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using LimaLims.Application.Service.File.Dto;
using LimaLims.Core.Entity.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace LimaLims.Application.Service.File
{
    /// <summary>
    /// 素材分组API
    /// </summary>
    [Route("api")]
    public class SysFileGroupService : ISysFileGroupService, IDynamicApiController, ITransient
    {
        private readonly IRepository<SysFileGroup> _sysFileGroupRep;
        private readonly IRepository<SysFile> _sysFileInfoRep;  // 文件信息表仓储
        public SysFileGroupService(IRepository<SysFileGroup> sysFileGroupRep, IRepository<SysFile> sysFileInfoRep)
        {
            _sysFileGroupRep = sysFileGroupRep;
            _sysFileInfoRep = sysFileInfoRep;
        }

        /// <summary>
        /// 新增分组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("sysFileGroup/add")]
        public async Task Add(FileGroupInput input)
        {
            var isExist = await _sysFileGroupRep.DetachedEntities.AnyAsync(u => u.Title == input.Title);
            if (isExist)
                throw Oops.Oh("分组已存在");
            var group = input.Adapt<SysFileGroup>();
            await group.InsertAsync();
        }

        /// <summary>
        /// 获取分组列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("sysFileGroup/GetList")]
        public async Task<List<FileGroupOutput>> GetList()
        {
            var groups = await _sysFileGroupRep.DetachedEntities.Where(u => u.ValidStatus).OrderBy(u => u.Sort)
                                                          .ProjectToType<FileGroupOutput>()
                                                          .ToListAsync();
            return groups;
        }
        /// <summary>
        /// 删除分组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("sysFileGroup/delete")]
        [UnitOfWork]
        public async Task Delete(long id)
        {
            var group = await _sysFileGroupRep.FirstOrDefaultAsync(u => u.Id == id);

            if (group == null)
                throw Oops.Oh("不存在此数据");

            var isExist = await _sysFileInfoRep.DetachedEntities
                                         .AnyAsync(u => u.SysFileGroupId == id);
            if (isExist) throw Oops.Oh("该分组存在素材数据！");

            await group.DeleteAsync();
        }

        /// <summary>
        /// 修改分组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("sysFileGroup/edit")]
        public async Task Update(FileGroupInput input)
        {
            var isExist = await _sysFileGroupRep.DetachedEntities.AnyAsync(u => u.Title == input.Title && input.Id != input.Id);
            if (isExist)
                throw Oops.Oh("该分组名称已存在");

            var group = await _sysFileGroupRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id.Equals(input.Id));
            if (group == null)
                throw Oops.Oh("不存在此数据！");

            var data = input.Adapt<SysFileGroup>();
            await data.UpdateAsync();

        }

    }
}
